﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Acr.XamForms.UserDialogs;
using SpotOffer.AppConstants;
using SpotOffer.DataModels;
using SpotOffer.Resource;
using SpotOffer.Services;
using SpotOffer.View;
using Xamarin;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;

namespace SpotOffer.ViewModels
{
    public class DealListViewModel : BaseViewModel
    {
		public DealListViewModel(Category category)
        {
           SelectedCategory = category;
           GetCity();
        }

        public List<Deal> DealList
        {
            get { return _dealList; }
            set
            {
                if (value != null)
                {
                    _dealList = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private List<Deal> _dealList;

        public List<Deal> DealListFiltered
        {
            get { return _dealListFiltered; }
            set
            {
                if (value != null)
                {
                    _dealListFiltered = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private List<Deal> _dealListFiltered;

        public Deal SelectedDeal
        {
            get
            {
                return _selectedDeal;
            }
            set
            {
                if (value != null)
                {
                    _selectedDeal = value;
                    Insights.Track("SelectedDeal", new Dictionary<string, string> { { "SelectedDeal", _selectedDeal.title }, });
                    _selectedDeal = null;
                    NotifyPropertyChanged();
                }
            }
        }
        private Deal _selectedDeal;


        public Category SelectedCategory
        {
            get { return _selectedCategory; }
            set
            {
                if (value != null)
                {
                    _selectedCategory = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private Category _selectedCategory;


        public bool DeleteButton
        {
            get { return _deleteButton; }
            set
            {
                _deleteButton = value;
                NotifyPropertyChanged();
            }
        }
        private bool _deleteButton;

        public string ResultLabel
        {
            get { return _numberofDeal; }
            set
            {
                _numberofDeal = value;
                NotifyPropertyChanged();
            }
        }
        private string _numberofDeal;

        public string Search
        {
            get { return _search; }
            set
            {
                _search = value;
                if (DealList != null & _search != null)
                {
                    DealListFiltered = DealList.Where(deal => deal.title.Trim().ToLower().Contains(_search.Trim().ToLower())).ToList();

                    if (_search.Length > 4)
                    {
                        Insights.Track("Searched", new Dictionary<string, string> { { "Searched", _search }, });
                    }

                }
                else if (_search.Length == 0 & DealList != null)
                {
                    DealListFiltered = DealList;
                }

                if (DealListFiltered != null)
                {
                    if (!DealListFiltered.Any())
                    {
                        ResultLabel = AppResources.SearchNoResult;
                    }
                    else
                    {
                        ResultLabel = SelectedCategory.name + " - " + DealListFiltered.Count + " " + AppResources.DealsFound;
                    }
                }
                DeleteButton = !string.IsNullOrEmpty(Search);
                NotifyPropertyChanged();
            }
        }
        private string _search;

        public Command LoadDealsCommand
        {
            get
            {
                return _loadDealsCommand ?? (_loadDealsCommand = new Command(ExecuteUpdateDealsCommand, () => !IsBusy));
            }
        }
        private Command _loadDealsCommand;

        private void ExecuteUpdateDealsCommand()
        {
            if (IsBusy)
                return;
            IsBusy = true;
            LoadDealsCommand.ChangeCanExecute();
            GetCity();
            IsBusy = false;
            LoadDealsCommand.ChangeCanExecute();
        }

        public async void GetCity()
        {
            Dialogs.ShowLoading(AppResources.Loading);
            try
            {
                var tempCity = App.Cache.Get<City>(CacheKeys.City);
                Title = tempCity.name;
                if (tempCity != null)
                {
                    var templist = await DependencyService.Get<IDealListService>().GetCity(tempCity.id);
                    // Check if new deals is selected or category
                    if (SelectedCategory.id.Equals("121"))
                    {
                        DealList = templist.deals.Where(deal => deal.is_new == 1).ToList();
                    }
                    else if (SelectedCategory.id.Equals("all"))
                    {
                        DealList = templist.deals;
                    }
                    else
                    {
                        DealList = templist.deals.Where(deal => deal.category.Equals(SelectedCategory.id)).ToList();
                    }
                    DealListFiltered = DealList;
                    ResultLabel = SelectedCategory.name + " - " + DealListFiltered.Count + " " + AppResources.DealsFound;
                    templist.categories.RemoveAll(c => c.count == 0);
                    foreach (var category in templist.categories)
                    {
                        category.name = category.name + " -" + category.count;
                    }
                    //MessagingCenter.Send<DealListViewModel,CityDeals>(this,MessagingConstants.Categories ,templist);

                  //  await Task.Delay(1000);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e + " " + e.Message);
                Dialogs.HideLoading();
                var ac = new AlertConfig { Message = e.Message };
                Dialogs.Alert(ac);
            }
            finally
            {
                Dialogs.HideLoading();
            //    App.AppInstance.MasterPageSpotOffer.IsPresented = false;
            }

        }

        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChanged();
            }
        }
        private string _title;


    }
}

